﻿using CustomPixelShaderEffect.UlitFolder;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Graphics.Canvas;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage.Streams;
using Windows.Storage;
using CustomPixelShaderEffect.AttributeUsageFloder;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace CustomPixelShaderEffect.views
{
    [PageType("填充或消除效果")]
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class FillOrDisappearPage : Page
    {
        PixelShaderEffect effect;
        //源图
        CanvasBitmap bitmap;
        CanvasRenderTarget render;
        public FillOrDisappearPage()
        {
            this.InitializeComponent();
            Init();
        }

        void Init()
        {
            canvas.CreateResources += async (s, e) =>
            {
                //获取着色器二进制文件
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Shaders/FillOrDisappearEffect.bin"));
                IBuffer buffer = await FileIO.ReadBufferAsync(file);
                //转换成字节数组
                var bytes = buffer.ToArray();
                //用 字节数组 初始化一个 PixelShaderEffect 对象；
                effect = new PixelShaderEffect(bytes);
            };

            //选择图片
            selectPicture.Click += async (s, e) =>
            {
                var file = await Ulit.SelectFileAsync(new List<string> { ".png", ".jpg" });
                if (file == null)
                {
                    render = null;
                    bitmap = null;
                }
                else
                {
                    bitmap = await CanvasBitmap.LoadAsync(canvas.Device, await file.OpenAsync(FileAccessMode.Read));
                    //创建一个与源图大小一样的幕后绘制画布；用于存放绘制效果；
                    render = new CanvasRenderTarget(canvas, bitmap.Size);
                }
                canvas.Invalidate();
            };

            canvas.Draw += (s, e) =>
            {
                //绘制黑白网格
                Win2dUlit.DrawGridGraphics(e.DrawingSession, 100);
                //判断effect 和位图是否为空
                if (effect == null || bitmap == null)
                    return;
                var element = (FrameworkElement)s;
                float effectWidth = (float)element.ActualWidth;
                float effectHeight = (float)element.ActualHeight * 0.7f;
                float previewWidth = effectWidth;
                float previewHeight = (float)element.ActualHeight * 0.3f;

                //将效果绘制到render上
                using (var ds = render.CreateDrawingSession())
                {
                    //设置效果参数
                    effect.Source1 = bitmap;
                    effect.Properties["time"] = (float)time.Value;
                    effect.Properties["model"] = (int)model.SelectedIndex;
                    ds.Clear(Colors.Transparent);
                    ds.DrawImage(effect);
                }
                ////绘制效果图到画布上
                var effectTran = Win2dUlit.CalcutateImageCenteredTransform(effectWidth, effectHeight, bitmap.Size.Width, bitmap.Size.Height);
                effectTran.Source = render;
                e.DrawingSession.DrawImage(effectTran);
                //绘制原图
                var previewTran = Win2dUlit.CalcutateImageCenteredTransform(previewWidth, previewHeight, bitmap.Size.Width, bitmap.Size.Height);
                previewTran.Source = bitmap;
                e.DrawingSession.DrawImage(previewTran, 0, effectHeight);
            };

            time.ValueChanged += (s, e) =>
            {
                canvas.Invalidate();
            };

        }
    }
}
